package com.gusha.controller;

import java.math.BigInteger;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysScenicDao;
import com.gusha.model.SysScenic;
import com.gusha.service.SysScenicService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/sysScenic")
public class SysScenicController {

	@Autowired
	private SysScenicDao sysScenicDao;
	
	@Autowired
	private SysScenicService sysScenicService;
	
    @RequestMapping("list")
    @SysOperateLog(operate = "查看景点列表")
	public String list(Model model, HttpServletRequest request) {
	      String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
	      String scenicName = WebValueUtil.getWebValue(request, "scenicName");

	        //参数封装
	        int pageNo = 0;
	        if (StringUtils.isNotBlank(pageIndex)) {
	            pageNo = Integer.parseInt(pageIndex);
	        }
	        	SysScenic sysScenic=new SysScenic();
	        	if (StringUtils.isNotBlank(scenicName)) {
	        		sysScenic.setScenicName(scenicName);
		        }
	        //获取分页数据
	        Sort sort = new Sort(Sort.Direction.DESC, "addTime");
	        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
	        Page<SysScenic> page = sysScenicService.findAll(pageable, sysScenic);
	        model.addAttribute("page", page);
		return "admin/sysScenic/list";
		
	}
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入景点编辑")
    public String toAdd(Model model) {
    	
		return "admin/sysScenic/form";
    	
    }
    
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入资讯编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {
        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");
        if (StringUtils.isNotBlank(id)) {
            if(sysScenicDao.existsById(new BigInteger(id))) {
                SysScenic sysScenic = sysScenicDao.getOne(new BigInteger(id));
                model.addAttribute("obj", sysScenic);
            }
        }  
        return "/admin/sysScenic/form";
    }
    
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存资讯信息")
    public JsonResult save(SysScenic sysScenic,HttpServletRequest request) {
        JsonResult result = new JsonResult(false,"保存失败");

        String scenicImgOld = WebValueUtil.getWebValue(request, "scenicImgOld");
        if(!scenicImgOld.equals(sysScenic.getScenicImg())) {
        	 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
             MultipartFile file = multipartRequest.getFile("file");
              // 要上传的目标文件存放路径
             String newFileName = FileUtils.getFileName(file.getOriginalFilename());
             String imagePath = Constants.getFilePath("info")+newFileName;
             String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");
             FileUtils.upload(file, localPath, newFileName);
             sysScenic.setScenicImg(imagePath);;
        }
        sysScenicDao.saveAndFlush(sysScenic);
        result.setFlag(true);
        result.setMsg("保存成功");
        return result;
    }
    
    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除景点信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
       
        if (sysScenicDao.existsById(id)) {
            SysScenic sysScenic = sysScenicDao.getOne(id);
            sysScenic.setIsDelete(new BigInteger("1"));
            sysScenicDao.saveAndFlush(sysScenic);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }


    // 恢复
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "恢复景点信息")
    public JsonResult recovery(BigInteger id) {
        JsonResult result = new JsonResult(false,"恢复失败");
        
        if (sysScenicDao.existsById(id)) {
        	SysScenic sysScenic = sysScenicDao.getOne(id);
        	sysScenic.setIsDelete(new BigInteger("0"));
            sysScenicDao.saveAndFlush(sysScenic);
            result.setFlag(true);
            result.setMsg("恢复成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }
}
